package fr.upmc.i3s.upmcontact.tools;

import java.util.Locale;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

public class StringUtils {

	public static String generateRandomString(int length) {
		    String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 
		    String pass = "";
		    for(int x=0;x<length;x++) {
		       int i = (int)Math.floor(Math.random() * 62);
		       pass += chars.charAt(i);
		    }
		    return pass;
	}
	
    public static String getRealPathFromURI(Activity activity, Uri contentUri) {
        try {
            String[] proj = {MediaStore.Images.Media.DATA};
            @SuppressWarnings("deprecation")
			Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        catch (Exception e) {
            return contentUri.getPath();
        }
    }
	
    public static String toProperNoun(String s) {
		char first = s.charAt(0);
		char[] next = new char[s.length()];
		next[0] = String.valueOf(first).toUpperCase(Locale.getDefault()).charAt(0);
		s.getChars(1, s.length(), next, 1);
		s = String.valueOf(next);
		return String.valueOf(next);
    }
    
	public static long getLongFromString(String string) {
		string = string.trim();
		if( string.contains( "." )){ 
			final int index = string.lastIndexOf( "." );
			return getLongFromString( string.substring( 0, index ))* 100 + getLongFromString( string.substring( index + 1 )); 
		}
		else { 
			return Long.valueOf( string ); 
		}
	}
    
}
